package com.koron.system.staff.controller;

import com.koron.bean.base.BaseQueryBean;
import com.koron.bean.base.Response;
import com.koron.common.core.business.system.tree.TreeUtil;
import com.koron.system.org.bean.UnifyOrgBean;
import com.koron.system.staff.bean.UnifyStaffBean;
import com.koron.system.staff.service.UnifyUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName UnifyUserController
 * @description
 * @Author zhouj
 * @Date 2021/7/8 14:05
 */
@Slf4j
@Api(tags = "统一平台用户及组织")
@RestController
@RequestMapping("/system/unifyUser")
public class UnifyUserController {

    @Autowired
    UnifyUserService unifyUserService;

    @ApiOperation(value = "获取组织树")
    @GetMapping("/getOrgNode")
    public Response getOrg(@RequestParam(required = false) Map<String, String> cond) {
        return Response.success(unifyUserService.getOrgNode(cond));
    }

    @ApiOperation(value = "获取组织树")
    @GetMapping("/getOrgNodeTree")
    public Response getOrgNodeTree(@RequestParam(required = false) Map<String, String> cond) {
        return Response.success(unifyUserService.getOrgNodeTree(cond));
    }

    @ApiOperation(value = "组织入库")
    @GetMapping("/inUpAllOrgNode")
    public Response inUpAllOrgNode(@RequestParam(required = false) Map<String, String> cond) {
        return Response.success("执行成功", unifyUserService.inUpAllOrgNode(cond));
    }

    @ApiOperation(value = "获取组织下人员列表")
    @GetMapping("/getStaff")
    public Response getStaff(@RequestParam(required = false) Map<String, String> cond) {
        return Response.success(unifyUserService.getStaff(cond));
    }

    @ApiOperation(value = "人员入库")
    @GetMapping("/inUpAllStaff")
    public Response inUpAllStaff(@RequestParam(required = false) Map<String, String> cond) {
        return Response.success("执行成功", unifyUserService.inUpAllStaff(cond));
    }

    @ApiOperation(value = "统一用户分页查询")
    @GetMapping("/queryStaffList")
    public Response queryStaffList(UnifyStaffBean unifyStaffBean, BaseQueryBean queryBean) {

        //支持级联查询  组织当传入条件
        List<UnifyOrgBean> unifyOrgBeans = unifyUserService.queryOrgNodeList(new UnifyOrgBean());
        List<UnifyOrgBean> arrayList = new ArrayList<>();
        if (!StringUtils.isEmpty(unifyStaffBean.getParentId())) {
            List<UnifyOrgBean> search = TreeUtil.search(unifyOrgBeans, new ArrayList(), unifyStaffBean.getParentId());
            TreeUtil.getList(search, arrayList);
        }
        List<String> orgIdList = arrayList.stream().map(UnifyOrgBean::getId).collect(Collectors.toList());
        unifyStaffBean.setOrgIdList(orgIdList);
        return Response.success(unifyUserService.queryStaffList(unifyStaffBean, queryBean), unifyUserService.queryStaffListCount(unifyStaffBean));
    }

    @ApiOperation(value = "统一平台组织查询")
    @GetMapping("/queryOrgNodeList")
    public Response queryOrgNodeList(UnifyOrgBean unifyOrgBean) {
        return Response.success(unifyUserService.queryOrgNodeList(unifyOrgBean));
    }

    @ApiOperation(value = "初始化集团组织及人员到自有组织,自有人员")
    @GetMapping("/initOrg")
    public Response initOrg(UnifyOrgBean unifyOrgBean) {
        return Response.success(unifyUserService.initOrg(unifyOrgBean));
    }


    @ApiOperation(value = "初始化集团组织及人员到自有组织")
    @GetMapping("/syncOrg")
    public Response syncOrg(UnifyOrgBean unifyOrgBean) {
        return unifyUserService.syncOrg(unifyOrgBean) > 0 ? Response.ok() : Response.fail("组织编码不存在！");
    }
}

